package Starter;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.*;

import Components.ImageButton;
import Components.RoomsPanel;
import Connector.Connector;

import smash_tris.*;

/** 
 * Klasa odpowiedzialna za wy�wietlanie Menu
 * @author Maciej Graszej
 * @author Pawe� Rych�y
 *
 */
public class Menu extends JPanel {
	
	
	
	/* Informacje o grze */
	private Player player;
	private ArrayList<Room> roomsList;
	
	/* Tło */
	private Image background;
	
	/* Elementy z menu */
	private ImageButton exit;
	private ImageButton create;
	private RoomsPanel roomsPane;
	private JScrollPane scroll;
	
	
	private static int width = 707;
	private static int heigh = 530;
	/* Reszta */
	private String DIR = "picture/menu_pics/";
	private boolean aLife = false;
	private MainFrame mainFrame = null;
	private Connector connector = null;

	public Menu(Player player, MainFrame mainFrame, ArrayList<Room> rooms) {
		this.setLayout(new GridBagLayout());
		
		this.mainFrame = mainFrame;
		this.connector = mainFrame.getConnector();
		mainFrame.setSize(width,heigh);
		this.roomsList = rooms;
		try {
			background = ImageIO.read(new File(DIR + "menu_background.jpg"));
		} catch (IOException e) {
			System.err.println("Błąd podczas załadowania tła w menu");
			e.printStackTrace();
		}
		
		this.setOpaque(true);
		initComponents();
		
	}

/*-----------METODY NADPISANE----------------------*/
	
    @SuppressWarnings("unchecked")	
	private void initComponents() {
    	
    	exit = new ImageButton(DIR+"exit_UP.png",DIR+"exit_DOWN.png");
    	exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connector.close();
            	System.exit(-1);
            }
        });

    	//exit.setBounds(width - 100, heigh - 100,62,62);
    	
    	create = new ImageButton(DIR+"create_UP.png",DIR+"create_DOWN.png");
    	create.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	if (connector!=null){
            		roomsList = connector.createRoom();
            	} else {
            		System.out.print("NULL");
            	}
            }
        });
    	
    	create.setBounds(width - 200, heigh - 300,62,62);

    	/*ArrayList<Room> rp = new ArrayList<Room>();
    	for(int i = 0; i < 5; i++)
    	rp.add(new Room());
    	*/

    	roomsPane = new RoomsPanel(this.roomsList, this);

		scroll = new JScrollPane(roomsPane);
		scroll.setPreferredSize(new Dimension(300, 200));
		
		scroll.setBounds(10, 10, 300, 400);
		
		
		JPanel pane = new JPanel();
		pane.setLayout(new GridLayout(1,2));
		pane.add(exit);
    	pane.add(create);
    	
    	GridBagConstraints scrollC = new GridBagConstraints();
		scrollC.weightx = 0.3;
		scrollC.gridx = 0;
		scrollC.gridy = 0;
    	this.add(scroll, scrollC);
    	
    	GridBagConstraints paneC = new GridBagConstraints();
		paneC.weightx = 0.3;
		paneC.gridx = 0;
		paneC.gridy = 2;
		this.add(pane, paneC);
		
    			
	}
    
    /**
     * Metoda rysujaca (g��wnie) t�o
     */
	@Override
	public void paintComponent(Graphics g) {
	    Graphics2D g2 = (Graphics2D) g;
	    
	    g2.drawImage(background, 0, 0, null);	    
	}

	
	
	/*@Override
	public void run() {
	aLife = true;
	
		while(aLife) {
			
		}

		
	}*/	
	

}
